// pages/Bmi_test/index.js\
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    height: '',
    weight: '',
    age: '',
    sex: '',
    hasClick: false,
    testSuccess: false,
    xingxUrl: '',
    nianling: ['18-39', '40-59', '60以上'],
    index: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sex: options.sex,
      type: options.type,
      id: options.id
    })
    console.log(options)
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  inputHeight(e) {
    let that = this
    console.log(e)
    that.setData({
      height: e.detail.value
    })
  },
  inputWeight(e) {
    let that = this
    // this.weight = e.detail.value
    that.setData({
      weight: e.detail.value
    })
  },
  inputAge(e) {
    let that = this
    // this.weight = e.detail.value
    that.setData({
      age: e.detail.value
    })
  },
  selectSex(e) {
    let that = this
    console.log(e)
    // this.sex = e.target.dataset.id
    that.setData({
      sex: e.target.dataset.id
    })
  },
  formSubmit () {
    let that = this
    if (that.data.hasClick) {
      return false
    }
    that.setData({
      hasClick: true
    })
    if (that.data.height == '') {
      wx.showToast({
        title: '请输入身高',
        icon: 'none'
      })
      that.setData({
        hasClick: false
      })
      return false
    } else if (that.data.weight == ''){
      wx.showToast({
        title: '请输入体重',
        icon: 'none'
      })
      that.setData({
        hasClick: false
      })
      return false
    //    else if (that.data.index == '') {
    //   wx.showToast({
    //     title: '请选择年龄',
    //     icon: 'none'
    //   })
    //   that.setData({
    //     hasClick: false
    //   })
    //   return false
    // }
    } else {
      let height = parseFloat(that.data.height / 100).toFixed(2)
      let BMI = parseFloat(parseFloat(that.data.weight) / (height * height)).toFixed(1)
      let fatRate = 0
      // if (that.data.sex == 1) {
      //   fatRate = parseFloat(1.2 * BMI + 0.23 * that.data.age - 5.4 - 10.8).toFixed(1)
      // } else {
      //   fatRate = parseFloat(1.2 * BMI + 0.23 * that.data.age - 5.4).toFixed(1)
      // }
      let pages = getCurrentPages()
      console.log(pages)
      if (that.data.type == 2) {
        app.request(app.apiDomain + 'index.php?c=bmi&a=setBmi', {
          sex: that.data.sex,
          age: '',
          height: height,
          weight:that.data.weight,
          bmi: BMI,
          fat_rate: fatRate
          }, function (res) {
          console.log(res)
          if (res.data.status == 1) {
            wx.showToast({
              title: '测试成功',
              duration: 1000
            })
            let leftMargin = 0
            let xingxUrl = ''
            let food_Url = ''
            let test_font = ''
            if (parseFloat(BMI) < 18.5) {
              leftMargin = parseFloat(BMI / 18.5 * 23).toFixed(1)
              test_font = '偏瘦'
              if (that.data.sex == 1) {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_ps.png'
              } else {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_ps.png'
              }
              food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/shucai.png'
            } else if (parseFloat(BMI) < 24) {
              test_font = '标准'
              leftMargin = parseFloat(parseFloat((BMI - 18.5) / 6.5) * 23) + 23
              if (that.data.sex == 1) {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_bz.png'
              } else {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_bz.png'
              }
              food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/zaji.png'
            } else if (parseFloat(BMI) < 28) {
              test_font = '超重'
              leftMargin = parseFloat(parseFloat((BMI - 24) / 4) * 23) + 46.1
              if (that.data.sex == 1) {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_cz.png'
              } else {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_cz.png'
              }
              food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/hanbao.png'
            } else {
              test_font = '肥胖'
              leftMargin = parseFloat(parseFloat((BMI - 28) / 10) * 31) + 69.2
              if (that.data.sex == 1) {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form.png'
                food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/pijiu.png'
              } else {
                xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form.png'
                food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/bingjil.png'
              }
            }
            that.setData({
              bmi: parseFloat(BMI),
              testSuccess: true,
              leftMargin: leftMargin >= 100 ? "100" : leftMargin,
              hasClick: false,
              height: '',
              weight: '',
              xingxUrl,
              food_Url,
              test_font
            })
          } else {
            wx.showToast({
              title: res.data.info,
              duration: 1000
            })
            that.setData({
              hasClick: false
            })
          }
        }, 'POST')
      } else if (that.data.type == 1){
        wx.showToast({
          title: '测试成功',
          duration: 1000
        })
        let height = parseFloat(that.data.height / 100)
        let BMI = parseFloat(parseFloat(that.data.weight) / (height * height)).toFixed(1)
        let leftMargin = 0
        let xingxUrl = ''
        let food_Url = ''
        let test_font = ''
        if (parseFloat(BMI) < 18.5) {
          leftMargin = parseFloat(BMI / 18.5 * 23).toFixed(1)
          test_font = '偏瘦'
          if (that.data.sex == 1) {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_ps.png'
          } else {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_ps.png'
          }
          food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/shucai.png'
        } else if (parseFloat(BMI) < 24) {
          test_font = '标准'
          leftMargin = parseFloat(parseFloat((BMI - 18.5) / 6.5) * 23) + 23
          if (that.data.sex == 1) {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_bz.png'
          } else {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_bz.png'
          }
          food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/zaji.png'
        } else if (parseFloat(BMI) < 28) {
          test_font = '超重'
          leftMargin = parseFloat(parseFloat((BMI - 24) / 4) * 23) + 46.1
          if (that.data.sex == 1) {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_cz.png'
          } else {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_cz.png'
          }
          food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/hanbao.png'
        } else {
          test_font = '肥胖'
          leftMargin = parseFloat(parseFloat((BMI - 28) / 10) * 31) + 69.2
          if (that.data.sex == 1) {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form.png'
            food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/pijiu.png'
          } else {
            xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form.png'
            food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/bingjil.png'
          }
        }
        that.setData({
          bmi: parseFloat(BMI),
          testSuccess: true,
          leftMargin: leftMargin >= 100 ? "100" : leftMargin,
          hasClick: false,
          height: '',
          weight: '',
          xingxUrl,
          food_Url,
          test_font
        })
      }
    }
    // wx.showToast({
    //   title: '测试成功',
    //   duration: 1000
    // })
    // let height = parseFloat(that.data.height / 100)
    // let BMI = parseFloat(parseFloat(that.data.weight) / (height * height)).toFixed(1)
    // let leftMargin = 0
    // let xingxUrl = ''
    // let food_Url = ''
    // let test_font = ''
    // if (parseFloat(BMI) < 18.5) {
    //   leftMargin = parseFloat(BMI / 18.5 * 23).toFixed(1)
    //   test_font = '偏瘦'
    //   if (that.data.sex == 1) {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_ps.png'
    //   } else {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_ps.png'
    //   }
    //   food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/shucai.png'
    // } else if (parseFloat(BMI) < 24) {
    //   test_font = '标准'
    //   leftMargin = parseFloat(parseFloat((BMI - 18.5) / 6.5) * 23) + 23
    //   if (that.data.sex == 1) {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_bz.png'
    //   } else {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_bz.png'
    //   }
    //   food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/zaji.png'
    // } else if (parseFloat(BMI) < 28) {
    //   test_font = '超重'
    //   leftMargin = parseFloat(parseFloat((BMI - 24) / 4) * 23) + 46.1
    //   if (that.data.sex == 1) {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form_cz.png'
    //   } else {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form_cz.png'
    //   }
    //   food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/hanbao.png'
    // } else {
    //   test_font = '肥胖'
    //   leftMargin = parseFloat(parseFloat((BMI - 28) / 10) * 31) + 69.2
    //   if (that.data.sex == 1) {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/male_fat_form.png'
    //     food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/pijiu.png'
    //   } else {
    //     xingxUrl = '//resource.160nurse.com/canvas/healthy/img/new_index/female_fat_form.png'
    //     food_Url = '//resource.160nurse.com/canvas/healthy/img/new_index/bingjil.png'
    //   }
    // }
    // that.setData({
    //   bmi: parseFloat(BMI),
    //   testSuccess: true,
    //   leftMargin: leftMargin >= 100 ? "100" : leftMargin,
    //   hasClick: false,
    //   height: '',
    //   weight: '',
    //   xingxUrl,
    //   food_Url,
    //   test_font
    // })
    // wx.setStorageSync('sex', that.data.sex)
    // wx.setStorageSync('bmi', BMI)
    // // wx.setStorageSync('xingxUrl', xingxUrl)
  },
  seeKnow() {
    let that = this
    that.setData({
      testSuccess: false,
      weight: '',
      height: ''
    })
    let pages = getCurrentPages()
    console.log(pages)
    if (that.data.type == 1) {
      wx.redirectTo({
        url: '../index/index',
      })
    } else {
      wx.redirectTo({
        url: '../Bmi_test/manageWeight?type=2&news=1&id=' + that.data.id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let that = this
    var nickName = '';
    var userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }
    return {
      title: '我的身体质量指数BMI:' + that.data.bmi + '属于' + that.data.test_font + '；建议你也测一测',
      path: '/pages/index/index?user_id=' + userId,
      imageUrl: app.resourceDomain + 'canvas/healthy/img/play/share.jpg'
    }
  }
})